<template>
  <d2-container>
    <div class="mt-3 ml-2">
      <el-form :model="form" label-width="80px">
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="微信支付" name="first">
            <div style="width: 30%;margin-top: 50px;">
              <el-form-item label="appId">
                <el-input v-model="form.wechat_appid" placeholder="小程序appId"></el-input>
              </el-form-item>
            </div>
            <div style="width: 30%;">
              <el-form-item label="商户号">
                <el-input v-model="form.wechat_mch_id" placeholder="微信商户号"></el-input>
              </el-form-item>
            </div>
            <div style="width: 60%;">
              <el-form-item label="支付密钥">
                <el-input v-model="form.wechat_api_key" placeholder="微信支付密钥"></el-input>
              </el-form-item>
            </div>
          </el-tab-pane>
          <div style="margin-top: 135px;">
            <el-form-item>
              <el-button type="primary" @click="submitForm">保存</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </div>
        </el-tabs>
      </el-form>
    </div>
  </d2-container>
</template>

<script>
  import {
    paymentconfigListUrl,
    paymentconfigCreateUrl,
    paymentconfigUpdateUrl,
    paymentconfigDetailUrl
  } from '@/utils/interface'
  export default {
    data() {
      return {
        paymentconfigList:[],
        paymentConfigId:'',
        activeName: 'first',
        form: {
          wechat_appid: '',
          wechat_mch_id: '',
          wechat_api_key: '',
        },
      }
    },
    mounted() {
      this.getPaymentconfigList()
    },
    methods: {
      getPaymentconfigList() {
        this.$get(paymentconfigListUrl).then(res => {
          this.paymentconfigList = res.data.data.list
          this.paymentConfigId = this.paymentconfigList[0].id
          this.getPaymentconfigDetail()
          console.log(this.paymentconfigList);
        })
      },
      getPaymentconfigDetail() {
        if (this.paymentconfigList.length !== 0) {
          this.$get(paymentconfigDetailUrl + this.paymentConfigId + '/')
            .then(res => {
              this.form = {
                wechat_appid: res.data.data.wechat_appid,
                wechat_mch_id: res.data.data.wechat_mch_id,
                wechat_api_key: res.data.data.wechat_api_key,
              }
              console.log('支付配置信息', res);
            })
            .catch(error => {
              console.log(error)
            })
        } else {
          return
        }
      },
      submitForm() {
        if(this.paymentconfigList.length == 0){
          this.$post(paymentconfigCreateUrl, this.form)
            .then(res => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
              this.getPaymentconfigDetail()
              this.getPaymentconfigList()
            })
            .catch(error => {
              console.log(error)
            })
        }else{
          this.$put(paymentconfigUpdateUrl + this.paymentConfigId + '/', this.form)
            .then(res => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
              this.getPaymentconfigDetail()
            })
            .catch(error => {
              console.log(error)
            })
        }
      },
    }
  }
</script>

<style>
</style>
